/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jesseenglish.swingftfy.extensions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Collections;
import java.util.LinkedList;
import javax.swing.JTextField;

/**
 *
 * @author jesse
 */
public class FTextField extends JTextField implements FocusListener, KeyListener {

    private String hintText;
    private LinkedList<String> autoCompletes;

    public FTextField() {
        super();

        this.hintText = "";
        this.autoCompletes = new LinkedList();
        
        this.addFocusListener(this);
        this.addKeyListener(this);
    }

    public void setHintText(String hintText) {
        this.hintText = hintText;
    }

    public String getHintText() {
        return this.hintText;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        if (this.hasFocus()) {
            return;
        }

        if (this.getText().trim().equalsIgnoreCase("")) {
            g.setColor(Color.LIGHT_GRAY);
            g.setFont(this.getFont());

            double middleOfTextField = ((double)this.getHeight()) / 2;
            double middleOfFont = ((double)g.getFontMetrics().getHeight()) / 3;

            int textLocation = (int)middleOfTextField + (int)middleOfFont;

            String toRender = this.hintText;
            
            if (toRender.equalsIgnoreCase("") || toRender == null) {
                return;
            }

            
            while (g.getFontMetrics().stringWidth(toRender) > Math.max((this.getWidth() - 14), 0)) {
                toRender = toRender.substring(0, toRender.length() - 1);
            }

            if (!toRender.equalsIgnoreCase(this.hintText) && toRender.length() > 3) {
                toRender = toRender.substring(0, toRender.length() - 3) + "...";
            }

            g.drawString(toRender, 7, textLocation);
        }
    }

    public void focusGained(FocusEvent fe) {
        this.validate();
        this.repaint();
    }

    public void focusLost(FocusEvent fe) {
        this.validate();
        this.repaint();
    }
    
    public void setAutoCompletes(LinkedList<String> autoCompletes) {
        this.autoCompletes = autoCompletes;
        Collections.sort(this.autoCompletes);
    }

    public void keyTyped(KeyEvent ke) {}

    public void keyPressed(KeyEvent ke) {}

    public void keyReleased(KeyEvent ke) {
        if (ke.getKeyCode() != KeyEvent.VK_SHIFT && ke.getKeyCode() != KeyEvent.VK_ENTER && ke.getKeyCode() != KeyEvent.VK_TAB && ke.getKeyCode() != KeyEvent.VK_BACK_SPACE && this.getText().length() > 0) {

            String text = this.getText();

            for (String auto : this.autoCompletes) {
                if (auto.startsWith(text)) {
                    this.setText(auto);


                    this.setCaretPosition(text.length());
                    this.setSelectionStart(text.length());
                    this.setSelectionEnd(auto.length());


                    this.validate();
                    this.repaint();

                    if (!auto.equals(text))
                        return;
                }
            }
        }
    }
    
}
